﻿using Kingdee.BOS.Core.Report.PlugIn;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Resource;
using Kingdee.K3.FIN.App.Core;
using Kingdee.K3.FIN.CB.App.Core;
using Kingdee.K3.FIN.CB.Common.BusinessEntity.FilterCondition;
using Kingdee.K3.FIN.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kingdee.K3.FIN.CB.Common.BusinessEntity;
using Kingdee.BOS.Orm.DataEntity;

namespace RK.ReportPlugin.Local
{
    public abstract class CBReportBaseService : AbstractCBReportService
    {
        private CommonBillHeadCondition _filterCondition
        {
            get;
            set;
        }

        protected bool HavePeriod
        {
            get;
            set;
        }

        protected bool HaveCurrency
        {
            get;
            set;
        }

        protected bool HaveActivityCost
        {
            get;
            set;
        }

        public override void BuilderReportSqlAndTempTable(IRptParams filter, string tableName)
        {
            InitCBParams();
            base.BuilderReportSqlAndTempTable(filter, tableName);
        }

        public override ReportTitles GetReportTitles(IRptParams filter)
        {
            ReportTitles val = (ReportTitles)(object)new ReportTitles();
            if (_filterCondition == null || !((AbstractFilterCondition)_filterCondition).IsValid())
            {
                return val;
            }
            DynamicObject customFilter = filter.FilterParameter.CustomFilter;
            val.AddTitle("FACCTGSYSTEMNAME", BillExtension.GetValue<DynamicObject>(customFilter, "ACCTGSYSTEMID")["Name"].ToString());
            val.AddTitle("FACCTGORGNAME", BillExtension.GetValue<DynamicObject>(customFilter, "ACCTGORGID")["Name"].ToString());
            val.AddTitle("FACCTPOLICYNAME", BillExtension.GetValue<DynamicObject>(customFilter, "ACCTPOLICYID")["Name"].ToString());
            if (HavePeriod)
            {
                if (_filterCondition.SelectYear == 0)
                {
                    _filterCondition.SelectYear = (BillExtension.GetValue<int>(customFilter, "Year"));
                    _filterCondition.SelectPeriod = (BillExtension.GetValue<int>(customFilter, "Period"));
                }
                val.AddTitle("FPERIOD", string.Format(ResManager.LoadKDString("{0}年{1}期", "003215000008917", (SubSystemType)4, new object[0]), _filterCondition.SelectYear, _filterCondition.SelectPeriod));
            }
            if (HaveCurrency)
            {
                val.AddTitle("FCurrencyNAME", BillExtension.GetValue<DynamicObject>(customFilter, "CurrencyID")["Name"].ToString());
            }
            return val;
        }

        public override void ConvertCustomFilterToCondition(IRptParams filter, AbstractFilterCondition condition)
        {
            CommonBillHeadCondition val = condition as CommonBillHeadCondition;
            if (filter != null && filter.FilterParameter != null && filter.FilterParameter.CustomFilter != null && val != null)
            {
                DynamicObject customFilter = filter.FilterParameter.CustomFilter;
                ((AbstractFilterCondition)val).AcntSystemId = (BillExtension.GetValue<long>(customFilter, "ACCTGSYSTEMID_Id"));
                ((AbstractFilterCondition)val).AcntOrgId = (BillExtension.GetValue<long>(customFilter, "ACCTGORGID_Id"));
                ((AbstractFilterCondition)val).AcntPolicyId = (BillExtension.GetValue<long>(customFilter, "ACCTPOLICYID_Id"));
                Tuple<int, int> systemCurrYearPeriod = CommonFunction.GetSystemCurrYearPeriod(((AbstractSysReportServicePlugIn)this).Context, ((AbstractFilterCondition)val).AcntSystemId, ((AbstractFilterCondition)val).AcntOrgId, ((AbstractFilterCondition)val).AcntPolicyId, "HS");
                val.CurYear = (systemCurrYearPeriod.Item1);
                val.CurPeriod = (systemCurrYearPeriod.Item2);
                val.SelectYear = (BillExtension.GetValue<int>(customFilter, "Year"));
                val.SelectPeriod = (BillExtension.GetValue<int>(customFilter, "Period"));
                ((AbstractFilterCondition)val).CurrencyId = (BillExtension.GetValue<long>(customFilter, "CurrencyID_Id"));
                _filterCondition = val;
                ActivityCostYesNo();
            }
        }

        protected virtual void InitCBParams()
        {
        }

        private void ActivityCostYesNo()
        {
            HaveActivityCost = CBAppCommFunction.GetActivityCostYesNo(((AbstractSysReportServicePlugIn)this).Context, ((AbstractFilterCondition)_filterCondition).AcntSystemId, ((AbstractFilterCondition)_filterCondition).AcntOrgId, ((AbstractFilterCondition)_filterCondition).AcntPolicyId);
        }
    }
}
